package com.bluedot.bank.action;

import java.util.Date;

import com.bluedot.bank.dao.account.Account;
import com.bluedot.bank.dao.account.AccountDao;
import com.bluedot.bank.dao.account.AccountDaoImpl;
import com.bluedot.bank.dao.exchangerecord.ExchangeRecord;
import com.bluedot.bank.dao.exchangerecord.ExchangeRecordDao;
import com.bluedot.bank.dao.exchangerecord.ExchangeRecordDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class ATMAction extends ActionSupport {
	private long id;
	private double balance;
	private String operation;
	private ExchangeRecord record =new ExchangeRecord();

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public ExchangeRecord getRecord() {
		return record;
	}

	public void setRecord(ExchangeRecord record) {
		this.record = record;
	}

	@Override
	public String execute() throws Exception {
		AccountDao accountDao=new AccountDaoImpl();
		ExchangeRecordDao recordDao=new ExchangeRecordDaoImpl();		
		Account account=accountDao.findById(id);
		if("atm存钱".equals(operation)){
			record.setAccount(account);
			record.setAmount(balance);
			record.setDescription(operation);
			account.setBalance(account.getBalance()+balance);
			record.setBalance(account.getBalance());
			record.setExchange_time(new Date());
			accountDao.saveOrUpdate(account);
			recordDao.saveOrUpdate(record);
		}else if("atm取钱".equals(operation)){
			record.setAccount(account);
			record.setAmount(balance);
			record.setDescription(operation);
			account.setBalance(account.getBalance()-balance);
			record.setBalance(account.getBalance());
			record.setExchange_time(new Date());
			accountDao.saveOrUpdate(account);
			recordDao.saveOrUpdate(record);
		}
		return SUCCESS;
	}
}
